// pages/dynamic/detail/detail.js
import {
  Dynamic
} from '../../../model/dynamic'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    commentValue: '',
    dynamicid: '',
    comment: null,
    ListArr: {},
    commentArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      dynamicid: options.dynamicid,
      comment: options.comment,
    })
    this.getDetailInfo(options.dynamicid)
    this.getCommentList(options.dynamicid)
  },
  async getDetailInfo(id) {
    const res = await Dynamic.getDynamicDetail({
      dynamicid: id
    })
    console.log(res);
    if (res.data) {
      this.setData({
        ListArr: res.data.data
      })
    }
  },
  userCommentInfo() {
    let auth = wx.$cache.get('userInfo')
    if (!auth) {
      wx.showToast({
        title: '请登录后再试',
        icon: 'none'
      })
      return;
    }
    if (!this.data.commentValue) {
      wx.showToast({
        title: '请输入您的见解',
        icon: 'none'
      })
      return;
    }
    let nick_name = wx.$cache.get('userInfo').nickName
    let avatar_url = wx.$cache.get('userInfo').avatarUrl
    let openid = wx.$cache.get('userInfo').openId
    let data = {
      content: this.data.commentValue,
      nick_name,
      avatar_url,
      dynamic_id: this.data.dynamicid,
      openid,
      comment: parseInt(this.data.comment)
    }
    wx.$http.post(wx.$post.postComment, data).then(res => {
      console.log(res);
      if (res.result) {
        this.getCommentList()
        this.getDetailInfo(this.data.dynamicid)
        this.setData({
          commentValue: ''
        })

      } else {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    })
  },
  getCommentList(id) {
    let ids = this.data.dynamicid || id
    wx.$http.get(wx.$get.getCommentList, {
      dynamicid: ids
    }).then(res => {
      console.log(res);
      if (res.result) {
        this.setData({
          commentArr: res.result
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})